<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class master_akun extends My_Controller
{
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('mdl_master_akun', 'master_akun');
		
	}
	
	function index()
	{
		$data['can_view'] 	= $this->can_view();
		$data['can_insert'] = $this->can_insert();
		$data['can_update'] = $this->can_update();
		$data['can_delete'] = $this->can_delete();
		
		$this->open();
		
		# config pagination
		$config['base_url'] = base_url().'index.php/master_akun/index/';
		$config['total_rows'] = sizeof($this->db->select('distinct(akunid),nakun')             ->from('master_akun')            ->get()			->result()); 
		$config['per_page'] = '30';		$config['num_links'] = '3';		$config['uri_segment'] = '3';				$config['full_tag_open'] = '';		$config['full_tag_close'] = '';				$config['num_tag_open'] = '<div id="page-info">';		$config['num_tag_close'] = '</div>';				$config['cur_tag_open'] = '<li class="page-current"><strong>';		$config['cur_tag_close'] = '</strong></li>';				$config['prev_link'] = '<li class="page-left"></li>';		$config['prev_tag_open'] = '';		$config['prev_tag_close'] = '';				$config['next_link'] = '<li class="page-right"></li>';		$config['next_tag_open'] = '';		$config['next_tag_close'] = '';				$config['last_link'] = '<li class="page-far-right"></li>';		$config['last_tag_open'] = '';		$config['last_tag_close'] = '';				$config['first_link'] = '<li class="page-far-left"></li>';		$config['first_tag_open'] = '';		$config['first_tag_close'] = '';

		$this->pagination->initialize($config);	
		# end config pagination
		
		# get data
		$data['results'] = $this->master_akun->getItem($config['per_page'], $this->uri->segment(3));
		
		# load view
		$this->load->view('master_akun/master_akun_list', $data);
		
		$this->close();
	}
	
	
	function insert()
	{
		if ($this->can_insert() == FALSE){
			redirect('auth/failed');
		}
		
		$this->open();
		# Data
		$data['akunid'] = $this->input->post('kode_akun');
		$data['nakun'] = $this->input->post('nama_akun');
		/*		$data['nama_cabang'] = $this->input->post('nama_cabang');
		$data['alamat'] = $this->input->post('alamat');
		$data['telepon'] = $this->input->post('telepon');
		$data['max_piutang'] = $this->input->post('max_piutang');
		$data['saldo_piutang'] = $this->input->post('saldo_piutang');
		$data['userid'] = get_userid();		*/
		
		
		# set rules validation
		$this->form_validation->set_rules('kode_akun', 'Kode Akun', 'required|numeric');
		$this->form_validation->set_rules('nama_akun', 'Nama Akun', 'callback_cek_nama|required');
		/*		$this->form_validation->set_rules('alamat', 'Alamat', 'trim');
		$this->form_validation->set_rules('telepon', 'telepon', 'trim|numeric');
		$this->form_validation->set_rules('max_piutang', 'max_piutang', 'trim|numeric');
		$this->form_validation->set_rules('saldo_piutang', 'saldo_piutang', 'trim|numeric');		*/
		
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		# set message validation
		$this->form_validation->set_message('required', 'Field %s harus diisi!');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');
		
		
		if ($this->form_validation->run() == FALSE){
			
			$this->load->view('master_akun/master_akun_add',$data);
			
		}else{	
			$this->master_akun->insert($data);
			$this->session->set_flashdata('message', 'Data Akun Berhasil disimpan.');
			redirect('master_akun');
		}
		
		$this->close();
	}
	
	function cek_nama($str)
	{
		if($str == ''){
			$this->form_validation->set_message('cek_nama', 'Field %s tidak boleh kosong.');
			return FAlSE;
		}
		
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->where('nama_cabang', $str);
		$q = $this->db->get('cabang');
		
		if($q->num_rows() > 0){
			$this->form_validation->set_message('cek_nama', 'Nama "'. $str . '" sudah terdaftar, coba masukan nama yang lain.');
			return FALSE;
		}else{
			return TRUE;
		}
		
	}
	
	function update($id)
	{
		if ($this->can_update() == FALSE){
			redirect('auth/failed');
		}
			
		$this->open();
		
		$data['result'] 		= $this->master_akun->getItemById($id);
		
		$data['akunid'] = $id;
		$data['akunid'] = $data['result']->row()->akunid;		$data['nakun'] = $data['result']->row()->nakun;		/*
		$data['alamat'] = $data['result']->row()->alamat;
		$data['telepon'] = $data['result']->row()->telepon;
		$data['max_piutang'] = $data['result']->row()->max_piutang;
		$data['saldo_piutang'] = $data['result']->row()->saldo_piutang;		*/
		
		
		
		$this->load->view('master_akun/master_akun_edit', $data);
		
		$this->close();
	}
	
	function process_update()	{				if ($this->can_update() == FALSE){			redirect('auth/failed');		}				$this->open();				# Data		$data['akunid'] = $this->input->post('akunid');		$data['nakun'] = $this->input->post('nakun');		/*		$data['nama_cabang'] = $this->input->post('nama_cabang');		$data['alamat'] = $this->input->post('alamat');		$data['telepon'] = $this->input->post('telepon');		$data['max_piutang'] = $this->input->post('max_piutang');		$data['saldo_piutang'] = $this->input->post('saldo_piutang');		$data['userid'] = get_userid();*/				# set rules validation		$this->form_validation->set_rules('akunid', 'Kode Akun', 'required|numeric');		$this->form_validation->set_rules('nakun', 'Nama Akun', 'required');/*		$this->form_validation->set_rules('alamat', 'Alamat', 'trim');		$this->form_validation->set_rules('telepon', 'telepon', 'trim|numeric');		$this->form_validation->set_rules('max_piutang', 'max_piutang', 'trim|numeric');		$this->form_validation->set_rules('saldo_piutang', 'saldo_piutang', 'trim|numeric');*/						$this->form_validation->set_error_delimiters('<div class="error">', '</div>');				# set message validation		$this->form_validation->set_message('required', 'Field %s harus diisi!');		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');						if ($this->form_validation->run() == FALSE){						$this->load->view('master_akun/master_akun_edit',$data);					}else{				$this->master_akun->update($data['akunid'], $data);			$this->session->set_flashdata('message', 'Data Akun Berhasil diupdate.');			redirect('master_akun');		}				$this->close();			}
	
	function delete($id)
	{
		if ($this->can_delete() == FALSE){
			redirect('auth/failed');
		}
		
		$this->master_akun->delete($id);
		$this->session->set_flashdata('message', 'Data Akun Berhasil dihapus.');
		redirect('master_akun');
	}
	
}